import {defineStore} from 'pinia'
import authData  from "@/mock/authData.js";

export const useAuthStore = defineStore('auth',{
    state:()=>({
        user:null,
        token:null,
        menuList:[],
        permissions:[],
        isAuthenticated:false,
        hasLoadedDynamicRoutes:false,
    }),
    actions:{
        login(user){
            console.log('user====',user)
            this.user  = user.data.user
            this.token = user.data.token
            //过滤处菜单中的子菜单种的 meta.hidden 为 true 的菜单
            // 递归过滤函数
            const filterHiddenMenus = (menus => {
                return menus.filter(menu => {
                    // 1. 过滤当前层级的隐藏项
                    if (menu.meta?.hidden) return false
                    // 2. 递归处理子菜单
                    if (menu.children?.length) {
                        menu.children = filterHiddenMenus(menu.children)
                    }
                    return true
                })
            });
            this.menuList = filterHiddenMenus(authData.data.menuList)
            this.permissions = authData.data.permissions
            this.isAuthenticated = true
        },
        getUser(){

        },
        logout(){
            this.user = null
            this.isAuthenticated = false
            this.token = null
            this.menuList = []
            this.permissions = []
            this.hasLoadedDynamicRoutes = false
        },
        isLoggedIn(){
            return this.isAuthenticated
        }
    }
})